﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using Microsoft.Phone;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using ProtoImaginer.Models;

namespace ProtoImaginer.ProtoImage
{
    public partial class AddPhoto : PhoneApplicationPage
    {
        CameraCaptureTask captureTask;
        PhotoChooserTask chooserTask;
        protected string id;
        protected Project projectToEdit = new Project();

        public AddPhoto()
        {
            InitializeComponent();
            textStatus.Text = "Tap the camera button to take a picture.";
            //Create new instance of CameraCaptureClass
            //Create new event handler for capturing a photo
            captureTask = new CameraCaptureTask();
            captureTask.Completed += new EventHandler<PhotoResult>(CameraCaptureTask_Completed);
            //Similar to capturing photos
            chooserTask = new PhotoChooserTask();
            chooserTask.Completed += new EventHandler<PhotoResult>(GalleryChooseTask_Completed);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            String parameterString = NavigationContext.QueryString["projectIdToSend"];
            id = parameterString;
            projectToEdit = MainPage.QueryProjectRecordById(id);
            ApplicationTitle.Text = projectToEdit.ProjectName;
        }

        void btnDiscard_Click(object sender, EventArgs e)
        {
            if (App.CapturedImage != null)
            {
                App.CapturedImage = null;
                MainImage.Source = null;
                textStatus.Text = "You must take a picture first.";
                Open_Camera.Visibility = Visibility.Visible;
                Open_Gallery.Visibility = Visibility.Visible;
                AddPhoto_ProgressBar.Visibility = Visibility.Collapsed;
            }
            else NavigationService.GoBack();
        }

        void btnEdit_Click(object sender, EventArgs e)
        {
            //Error text for if user does not take a photo before choosing the crop button.
            if (App.CapturedImage == null)
            {
                textStatus.Text = "You must take a picture first.";

            }
            else
            {
                //If photo has been taken, crop button navigates to Crop.xaml page.
                NavigationService.Navigate(new Uri("/ProtoImage/ModifyPhoto.xaml", UriKind.Relative));
            }
        }
        
        void CameraCaptureTask_Completed(object sender, PhotoResult e)
        {

            if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
            {
                //Take JPEG stream and decode into a WriteableBitmap object
                App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
                //Collapse visibility on the progress bar once writeable bitmap is visible.
                AddPhoto_ProgressBar.Visibility = Visibility.Collapsed;
                //Populate image control with WriteableBitmap object.
                
                WriteableBitmap wBitmap = new WriteableBitmap(App.CapturedImage);
                
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("wBitmap", FileMode.Create, isf))
                    {
                        wBitmap.SaveJpeg(isfs, 1600, 1200, 0, 100);
                        App.CapturedImage.SetSource(isfs);
                        isfs.Close();
                    }
                }
                MainImage.Source = App.CapturedImage;
                //Once writeable bitmap has been rendered, the crop button is enabled.
                textStatus.Text = "Tap the crop button to proceed";
            }
            else
            {
                textStatus.Text = "You decided not to take a picture.";
            }
        }

        void GalleryChooseTask_Completed(object sender, PhotoResult e)
        {
            //Take JPEG stream and decode into a WriteableBitmap object
            App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
            //Collapse visibility on the progress bar once writeable bitmap is visible.
            AddPhoto_ProgressBar.Visibility = Visibility.Collapsed;
            //Populate image control with WriteableBitmap object.
            MainImage.Source = App.CapturedImage;
            //Once writeable bitmap has been rendered, the crop button is enabled.
            textStatus.Text = "Tap the crop button to proceed";
        }

        private void Open_Gallery_Click(object sender, MouseButtonEventArgs e)
        {
            textStatus.Text = "";
            chooserTask.Show();
            Open_Camera.Visibility = Visibility.Collapsed;
            Open_Gallery.Visibility = Visibility.Collapsed;
            AddPhoto_ProgressBar.Visibility = Visibility.Visible;
        }

        private void Open_Camera_Click(object sender, MouseButtonEventArgs e)
        {
            textStatus.Text = "";
            captureTask.Show();
            Open_Camera.Visibility = Visibility.Collapsed;
            Open_Gallery.Visibility = Visibility.Collapsed;
            AddPhoto_ProgressBar.Visibility = Visibility.Visible;

        }
    }
}
